//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "TermStructureFittingParameter.h"
using namespace Cephei::QL::Models;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/Parameter.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::CTermStructureFittingParameter::CTermStructureFittingParameter (Cephei::QL::Termstructures::IYieldTermStructure^ term) : CParameter(CTermStructureFittingParameter::typeid)
{
    CYieldTermStructure^ _Cterm;
    try
    {
#ifdef HANDLE
        _phTermStructureFittingParameter = NULL;
#endif
        _Cterm = safe_cast<CYieldTermStructure^> (term);
        _Cterm->Lock();
        Handle<QuantLib::YieldTermStructure>& _term = static_cast<Handle<QuantLib::YieldTermStructure>&> (_Cterm->GetHandle ()); 
        _ppTermStructureFittingParameter = new boost::shared_ptr<QuantLib::TermStructureFittingParameter> (new QuantLib::TermStructureFittingParameter ( _term ));
        SetParameter (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppTermStructureFittingParameter));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cterm != nullptr) _Cterm->Unlock();
    }
}
Cephei::QL::Models::CTermStructureFittingParameter::CTermStructureFittingParameter (boost::shared_ptr<QuantLib::TermStructureFittingParameter>& childNative, Object^ owner) : CParameter(CTermStructureFittingParameter::typeid)
{
#ifdef HANDLE
	_phTermStructureFittingParameter = NULL;
#endif
	_ppTermStructureFittingParameter = &childNative;
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppTermStructureFittingParameter));
}
Cephei::QL::Models::CTermStructureFittingParameter::CTermStructureFittingParameter (QuantLib::TermStructureFittingParameter& childNative, Object^ owner) : CParameter(CTermStructureFittingParameter::typeid)
{
#ifdef HANDLE
	_phTermStructureFittingParameter = NULL;
#endif
	_ppTermStructureFittingParameter = new boost::shared_ptr<QuantLib::TermStructureFittingParameter> (&childNative);
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppTermStructureFittingParameter));
    _TermStructureFittingParameterOwner = owner;
    _ParameterOwner = owner;
}

Cephei::QL::Models::CTermStructureFittingParameter::CTermStructureFittingParameter (CTermStructureFittingParameter^ copy) : CParameter(CTermStructureFittingParameter::typeid)
{
#ifdef HANDLE
	_phTermStructureFittingParameter = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTermStructureFittingParameter = new boost::shared_ptr<QuantLib::TermStructureFittingParameter> (copy->GetShared());
        _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppTermStructureFittingParameter));
    }
}
Cephei::QL::Models::CTermStructureFittingParameter::CTermStructureFittingParameter (PLATFORM::Type^ t) : CParameter(CTermStructureFittingParameter::typeid)
{
#ifdef HANDLE
	_phTermStructureFittingParameter = NULL;
#endif
	if (!t->IsSubclassOf(CTermStructureFittingParameter::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::CTermStructureFittingParameter::CTermStructureFittingParameter (QuantLib::Handle<QuantLib::TermStructureFittingParameter>& childNative, Object^ owner)  : CParameter(CTermStructureFittingParameter::typeid)
{
	_phTermStructureFittingParameter = &childNative;
	_ppTermStructureFittingParameter = &static_cast<boost::shared_ptr<QuantLib::TermStructureFittingParameter>>(childNative.currentLink());
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppTermStructureFittingParameter));
    _TermStructureFittingParameterOwner = owner;
}
Cephei::QL::Models::CTermStructureFittingParameter::CTermStructureFittingParameter (QuantLib::Handle<QuantLib::TermStructureFittingParameter> childNative)  : CParameter(CTermStructureFittingParameter::typeid)
{
	_phTermStructureFittingParameter = &childNative;
	_ppTermStructureFittingParameter = &static_cast<boost::shared_ptr<QuantLib::TermStructureFittingParameter>>(childNative.currentLink());
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppTermStructureFittingParameter));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::CTermStructureFittingParameter::CTermStructureFittingParameter (QuantLib::TermStructureFittingParameter childNative)  : CParameter(CTermStructureFittingParameter::typeid)
{
#ifdef HANDLE
	_phTermStructureFittingParameter = NULL;
#endif
	_ppTermStructureFittingParameter = new boost::shared_ptr<QuantLib::TermStructureFittingParameter> (new QuantLib::TermStructureFittingParameter (childNative));
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppTermStructureFittingParameter));
}
#endif

Cephei::QL::Models::CTermStructureFittingParameter::~CTermStructureFittingParameter ()
{
    if (_ppTermStructureFittingParameter != NULL)
    {
	    delete _ppTermStructureFittingParameter;
        _ppTermStructureFittingParameter = NULL;
    }
}
Cephei::QL::Models::CTermStructureFittingParameter::!CTermStructureFittingParameter ()
{
    if (_ppTermStructureFittingParameter != NULL)
    {
	    delete _ppTermStructureFittingParameter;
    }
}
QuantLib::TermStructureFittingParameter& Cephei::QL::Models::CTermStructureFittingParameter::GetReference ()
{
    if (_ppTermStructureFittingParameter == NULL) throw REFNEW NativeNullException ();
	return **_ppTermStructureFittingParameter;
}
boost::shared_ptr<QuantLib::TermStructureFittingParameter>& Cephei::QL::Models::CTermStructureFittingParameter::GetShared ()
{
    if (_ppTermStructureFittingParameter == NULL) throw REFNEW NativeNullException ();
	return *_ppTermStructureFittingParameter;
}
QuantLib::TermStructureFittingParameter* Cephei::QL::Models::CTermStructureFittingParameter::GetPointer ()
{
    if (_ppTermStructureFittingParameter == NULL) throw REFNEW NativeNullException ();
	return &**_ppTermStructureFittingParameter;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TermStructureFittingParameter>& Cephei::QL::Models::CTermStructureFittingParameter::GetHandle ()
{
	if (_phTermStructureFittingParameter == NULL)
	{
		_phTermStructureFittingParameter = new Handle<QuantLib::TermStructureFittingParameter> (*_ppTermStructureFittingParameter);
	}
	return *_phTermStructureFittingParameter;
}
#endif
bool Cephei::QL::Models::CTermStructureFittingParameter::HasNative () 
{
	return (_ppTermStructureFittingParameter != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::ITermStructureFittingParameter^ Cephei::QL::Models::CTermStructureFittingParameter_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ term)
{
    return REFNEW CTermStructureFittingParameter ( term);
}
